package com.panda.web.controller.aims;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/web/callbackHandler")

public class CallbackHandler {

    private static final Logger logger = LoggerFactory.getLogger(CallbackHandler.class);

    public static void main(String[] args) {
        SpringApplication.run(CallbackHandler.class, args);
    }

    @PostMapping("/huidiao")
    public ResponseEntity<String> handlePost(@RequestHeader(value = "Authorization", required = false) String authorizationHeader,
                                             @RequestBody Map<String, Object> callbackData) {
        // 检查 Authorization 头部
        if (authorizationHeader == null || !authorizationHeader.startsWith("Bearer fixed-token")) {
            logger.error("Unauthorized request");
            return new ResponseEntity<>("Unauthorized", HttpStatus.UNAUTHORIZED);
        }

        try {
            // 记录接收到的数据
            logger.info("Parsed JSON data:");
            logger.info(callbackData.toString());

            // 返回成功响应
            return new ResponseEntity<>("Callback received successfully", HttpStatus.OK);
        } catch (Exception e) {
            // 处理异常
            logger.error("Error processing request", e);
            return new ResponseEntity<>("Invalid JSON", HttpStatus.BAD_REQUEST);
        }
    }
}
